/**
 * 
 */
package com.piz.web.product;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.piz.model.ItemClassification;
import com.piz.model.ItemDetails;
import com.piz.service.common.UnexpectedServiceException;
import com.piz.service.model.ItemBean;
import com.piz.service.productservice.ProductMgmtService;
import com.piz.service.productservice.ProductService;

/**
 * @author Administrator
 *
 */
@Controller
public class ProductCategoryController {
	
	@Autowired
	ProductMgmtService productMgmtService;
	
	@Autowired
	ProductService productService;
	
	@Autowired
	ProductCategoryValidation productCategoryValidation;
	
	@RequestMapping(value="viewClass",method = RequestMethod.GET)
	public ModelAndView viewClass(@RequestParam(value = "id", defaultValue = "0.0") Long id,
			ItemClassification itemCat, Model model, HttpServletRequest request)
	{
		Map<String, Object> refrenceData = new HashMap<String, Object>();
		
		if(id != null && id > 0.0)
		{
			itemCat = productMgmtService.fetchProductClassById(id);
			refrenceData.put("edit", true);
		}
		if(itemCat == null)
		{
			itemCat = new ItemClassification();
		}
		refrenceData.put("itemClassification", itemCat);
		model.addAllAttributes(refrenceData);
		return new ModelAndView("admin/viewCat");
	}
	
	@RequestMapping(value="addCat",method = RequestMethod.POST)
	public String saveCategory(HttpServletRequest request, @Valid  @ModelAttribute ItemClassification itemClassification,BindingResult result,Map<String, Object> model)
	{
		productCategoryValidation.validate(itemClassification, result);
		
		if(result.hasErrors())
		{
			model.put("itemClassification", itemClassification);
			return "admin/viewCat";
		}
		try {
			productMgmtService.addProductClass(itemClassification);
			productMgmtService.saveProductClassImage(request.getRealPath(""), itemClassification);
		} catch (UnexpectedServiceException e) {
			e.printStackTrace();
		}
		return "redirect:categoryList.action";
	}
	
	@RequestMapping(value = "deleteClass" , method = RequestMethod.GET)
	public ModelAndView deleteProduct(@RequestParam(value="id", defaultValue = "0.0") Long id,Model model,HttpServletRequest request) 
	{
		productMgmtService.deleteProductClass(id);
		return new ModelAndView("redirect: categoryList.action");
	}
	
	@RequestMapping(value = "categoryList" , method = RequestMethod.GET)
	public ModelAndView productList(Model model, HttpServletRequest request)
	{
		List<ItemClassification> categories;
		try {
			categories = productService.getAllCategories();
		} 
		catch (Exception e) {
			throw new UnexpectedServiceException("Exception in getting product list");
		}
		
		Map<String, Object> refrenceData = new HashMap<String, Object>();
		refrenceData.put("classes", categories);
		model.addAllAttributes(refrenceData);
		return new ModelAndView("admin/catList");
	}

}
